/**
 * 
 */
package com.pks.autoInsurance.helper;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import com.pks.autoInsurance.dao.CustomerDAO;
import com.pks.autoInsurance.dao.CustomerDAOImpl;
import com.pks.autoInsurance.dao.VehicleDAO;
import com.pks.autoInsurance.dao.VehicleDAOImpl;
import com.pks.autoInsurance.dto.CustomerDto;
import com.pks.autoInsurance.dto.VehicleDto;

/**
 * Contain user related methods.
 * 
 * @author Pankaj Soni
 * 
 */
public class UserHelper {

	private static final Logger LOGGER = Logger.getLogger(UserHelper.class);

	/**
	 * Returns user profile
	 * 
	 * @param ssn
	 * @return RegistrationDto
	 */
	public CustomerDto getUserProfile(String ssn) {
		LOGGER.debug("begin");
		LOGGER.info("ssn: " + ssn);
		CustomerDAO customerDao = new CustomerDAOImpl();
		return customerDao.findCustomer(ssn);
	}

	/**
	 * Retrives premium quote for a user
	 * 
	 * @param ssn
	 * @return quote for vehicle
	 */
	public Map<String, Object> getQuote(String ssn) {
		LOGGER.debug("begin");
		CustomerDAO customerDAO = new CustomerDAOImpl();
		CustomerDto customer = customerDAO.findCustomer(ssn);
		VehicleDAO vehicleDAO = new VehicleDAOImpl();
		VehicleDto vehicle = vehicleDAO.findVehicle(ssn);
		Map<String, Object> map = new HashMap<String, Object>();
		long timediff = new Date().getTime()
				- customer.getDateOfBirth().getTime();
		int convFactor = 1000 * 3600 * 24 * 365;
		float age = (float) timediff / convFactor;
		int totalAccident = vehicle.getTotalAccident();
		int policyAmount = vehicle.getPolicyAmount();
		float riskFactor = age / 210 + (float) totalAccident / 10;
		float premiumAnnually = policyAmount * riskFactor;
		float premiumQuarterly = premiumAnnually / 4;
		float premiumMonthly = premiumAnnually / 12;
		map.put("userInfo", customer);
		map.put("vehicleInfo", vehicle);
		map.put("premiumAnnually", String.valueOf((int) premiumAnnually));
		map.put("premiumQuarterly", String.valueOf((int) premiumQuarterly));
		map.put("premiumMonthly", String.valueOf((int) premiumMonthly));
		LOGGER.info("map: "+map);
		LOGGER.debug("end");
		return map;
	}

}
